package org.NooLab.itexx.stuctures;

import java.io.Serializable;
import java.util.ArrayList;

import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.ITexxWindowIntf;
import org.NooLab.itexx.app.gui.definition.ITexxWindowGuiIntf;
import org.NooLab.itexx.app.storage.GuiPersistence;
import org.NooLab.utilities.strings.ArrUtilities;
import org.NooLab.utilities.strings.StringsUtil;





public class RequestFocusContext 
									implements
												Serializable,
												RequestFocusContextIntf{

	boolean isActive = false;
	
	transient ITexxWindowIntf webBrowserApp ;
	transient ITexxWindowGuiIntf windowGui;
	transient ITexxWindowCommons appCommons;
	
	GuiPersistence guiPersistence;
	GuiPersistenceData guiData, lastGuiData;

	// these are updated on event or on request
	RequestFocusSpec requestFocusSpec;
	
	transient StringsUtil strgutil = new StringsUtil();

	private boolean requestFocusIsEdited;

	
	// ========================================================================
	public RequestFocusContext(ITexxWindowIntf app){
		// this is owned by "ItexxModelController", which in turn is owned by the windows component
		webBrowserApp = app;
		windowGui = app.getWindowGui();
		appCommons = app.getAppCommons() ;
		
		requestFocusSpec = new RequestFocusSpec();
		guiPersistence = GuiPersistence.getInstance(app, windowGui) ; 
	}
	// ========================================================================
	
	
	
	
	public GuiPersistence getGuiPersistence() {
		return guiPersistence;
	}


	public GuiPersistenceData updateFocusDataFromGui() {
		
		String str;
		// GuiPersistenceData guiData = new GuiPersistenceData();
		GuiPersistenceData prevGuiFocusData ;
		
		
		// this is for storage ???
		prevGuiFocusData = guiPersistence.getGuiData();
		
		if ((this.guiData==null) || (guiData!=guiPersistence.getGuiData())){
			guiData = guiPersistence.getGuiData();
		}
		
		// this is from gui
		str = guiData.getFocusNormal() ;
		
		// TextNormal, TextMandatory, TextExclude, TextTolerated
		str = windowGui.getTextNormal().getText();
		guiData.setFocusNormal( str ) ;
		
		str = windowGui.getTextMandatory().getText();
		guiData.setFocusMandatory( str ) ;
		
		str = windowGui.getTextExclude().getText();
		guiData.setFocusExcluding( str ) ;
		
		// note that the tolerated tab might be removed!
		if (windowGui.getTextTolerated()!=null ){
			str = windowGui.getTextTolerated().getText();
			guiData.setFocusTolerated( str ) ;
		}
		// this is for storage ???
		guiPersistence.setGuiData(guiData);
		
		return guiData;
	}


	public void prepareItems() {
		int p ;
		boolean updateM=false, updateN=false;
		String xstr ;
		
		
		if ((guiData==null) && (guiPersistence.getGuiData()==null)){
			return;
		}
		if ((guiData==null) || (guiData!=guiPersistence.getGuiData())){
			guiData = guiPersistence.getGuiData();
		}
			// 
		updateFocusDataFromGui();
		
			String focusMandStr = getGuiData().getFocusMandatory();
			String focusExclStr = getGuiData().getFocusExcluding();
			String focusNormStr = getGuiData().getFocusNormal();
			
	
			requestFocusSpec.mandatoryItems= prepareFocusItem(focusMandStr);
			requestFocusSpec.normalItems= prepareFocusItem(focusNormStr);
			requestFocusSpec.excludingItems= prepareFocusItem(focusExclStr);
			
			for (int i=0;i<requestFocusSpec.excludingItems.size();i++){
				
				xstr = requestFocusSpec.excludingItems.get(i).trim();
				
				p = requestFocusSpec.normalItems.indexOf( xstr );
				if ((p>=0) && (xstr.length()>0)){
					requestFocusSpec.normalItems.remove(p);
					updateN=true;
				}
				p = requestFocusSpec.mandatoryItems.indexOf( xstr );
				if ((p>=0) && (xstr.length()>0)){
					requestFocusSpec.mandatoryItems.remove(p);
					updateM=true;
				}
	
			}
			// note that the tolerated tab might have been removed!
			if (updateM){
				// windowGui. 
			}
			if (updateN){
				
			}
			 		
	}




	public void updateRequestFocusData(ITexxWindowGuiIntf windowGui) {
		// 
		// first we get the basic data ...
		updateFocusDataFromGui();
		
		/* and then we have to process it:
		 * 	- extracting
		 *  - checking contradictions across categories
		 *      ->-> order of significance: mandatory, exclusive, normal, tolerated
		 *  - extracting info from delivered file
		 *  - packaging, and in case of a file caching...
		 */
		
	}



	public boolean isActive() {
		return isActive;
	}


	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public GuiPersistenceData getGuiData() {
		return guiData;
	}


	public void setGuiData(GuiPersistenceData guiData) {
		this.guiData = guiData;
	}


	public void setGuiPersistence(GuiPersistence guiPersistence) {
		this.guiPersistence = guiPersistence;
	}


	private ArrayList<String> prepareFocusItem(String fStr) {
		// 
		
		fStr = strgutil.replaceAll(fStr, "\r", " ");
		fStr = strgutil.replaceAll(fStr, "\r\n", " ");
		fStr = strgutil.replaceAll(fStr, "\n", " ");
		
		fStr = strgutil.replaceAll(fStr, ",", " ");
		fStr = strgutil.replaceAll(fStr, ";", " ");
		fStr = strgutil.replaceAll(fStr, ":", " ");
		
		fStr = strgutil.replaceAll(fStr, "  ", " ");
		
		String[] items = fStr.split(" ");
		
		ArrayList<String> fitems = ArrUtilities.changeArraystyle(items) ; 
		
		fitems = ArrUtilities.removeEntries(fitems,new String[]{""});
		
		return fitems;
	}

	public ArrayList<String> getMandatoryItems() {
		return requestFocusSpec.mandatoryItems;
	}

	public ArrayList<String> getNormalItems() {
		return requestFocusSpec.normalItems;
	}

	public ArrayList<String> getExcludingItems() {
		return requestFocusSpec.excludingItems;
	}

	public void setMandatoryItems(ArrayList<String> mandatoryItems) {
		this.requestFocusSpec.mandatoryItems = mandatoryItems;
	}

	public void setNormalItems(ArrayList<String> normalItems) {
		this.requestFocusSpec.normalItems = normalItems;
	}

	public void setExcludingItems(ArrayList<String> excludingItems) {
		this.requestFocusSpec.excludingItems = excludingItems;
	}




	public RequestFocusSpec getRequestFocusSpec() {
		return requestFocusSpec;
	}




	public void setRequestFocusSpec(RequestFocusSpec requestFocusSpec) {
		this.requestFocusSpec = requestFocusSpec;
	}



	/**
	 * setting a edited marker to false, and saving the current RequestFocus,
	 * in order to be able to detect any change
	 * 
	 */
	public void setEdited(boolean flag) {
		// 
		requestFocusIsEdited = flag;
		if (flag==false){
			lastGuiData = new GuiPersistenceData( guiData );
		}
	}
	public boolean isEdited() {
		
		boolean hb = requestFocusIsEdited;
		
		if (lastGuiData!=null){
			if ((requestFocusIsEdited) ||
				(lastGuiData.isEqual(guiData) == false)) {
				hb = true;
			}
		}
		return hb;
	}

	public boolean isRequestFocusIsEdited() {
		return requestFocusIsEdited;
	}
	public void setRequestFocusIsEdited(boolean flag) {
		this.requestFocusIsEdited = flag;
	}
	
	
	
}
